<?php

namespace app\libs\AsiabillSDK\builder;

use app\constant\CommonConstant;
use app\model\Orders;

class CheckoutBuilder extends BuilderBase
{
    private $rate = 0.15;

    public function toArray()
    {
        $requestData = [
            'callbackUrl'=>$this->order->notify_url,
            'customerPaymentMethodId'=>$this->getCustomerPaymentMethodId(),
            'isMobile'=>'0',
            'customerIp'=>self::CUSTOMER_CLIENT_IP,
            'orderAmount'=> $this->getTotalMoney(),
            'orderCurrency'=>$this->order->present_currency,
            'platform'=>'php_SDK',
            'remark'=>strval($this->order->id),
            'returnUrl'=>$this->getReturnUrl(),
            'webSite'=>$this->order->shop->website ?: $this->order->shop->host,
            'shipping'=>$this->getShipping(),
            'goodsDetails'=>$this->getGoodsDetails(),
            'orderNo'=>$this->order->order_no,//订单号
        ];
        if($this->getCustomerId()) $requestData['customerId'] = $this->getCustomerId();
        return $requestData;
    }



    public function getGoodsDetails()
    {
        $products = [];
        $items = Orders::getOrdersLineItems($this->order);
        foreach ($items as $item){
            $products[] = [
                'goodsCount' => $item['quantity'],
                'goodsPrice' => $item['price'],
                'goodsTitle' => $item['title']
            ];
        }
        if($this->order->shipping_price_present > 0){

            $products[] = [
                'goodsCount' => 1,
                'goodsPrice' => $this->order->shipping_price_present,
                'goodsTitle' => $this->order->shipping_name,
            ];
        }
        //税费
//        $products[] = [
//            'goodsCount'=>1,
//            'goodsPrice'=>bcmul($this->order->total_price,$this->rate,2),
//            'goodsTitle'=>"Tax",
//        ];


        return $products;
    }

    protected function getTotalMoney()
    {
        list($total_money,$tax_price) = Orders::calculateTotalPrice($this->order->total_price);
        return $total_money;
    }

}